package jobhunter.repository;

import jobhunter.model.Company;
import jobhunter.security.SecurityContext;

import org.springframework.stereotype.Repository;

@Repository
@SuppressWarnings("unchecked")
public class CompanyRepository extends GenericRepositoryJpa<Company> 
{
	public CompanyRepository() 
	{
		setEntityClass(Company.class);
	}
	public Company findByIdWithContactAndProcess(Long id)
	{
		return (Company)em.createQuery("SELECT c FROM Company c LEFT JOIN FETCH c.contacts LEFT JOIN FETCH c.processes Where c.id = :id AND c.user=:user")
				.setParameter("id", id).setParameter("user", SecurityContext.getUser()).getSingleResult();
	}

	@Override
	public Company merge(Company t) 
	{
		if(t.getUser()==SecurityContext.getUser())
			return em.merge(t);
		else
			return null;
	}

	@Override
	public void remove(Company t) 
	{
		if(t.getUser()==SecurityContext.getUser())
			em.remove(t);		
	}
}
